<template>
  <div class="stockUp">
    <!-- 搜索框 -->
    <div v-if="search_visible" id="search" class="search">
      <el-form :model="searchData">
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item>
              <el-input
                id="company_name"
                v-model="searchData.product_title"
                placeholder="请输入商品名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-select
                style="display: block"
                v-model="searchData.order_status"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option key="0" label="开启" value="1"></el-option>
                <el-option key="1" label="锁住" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <search-tool
                :search_data.sync="searchData"
                @refesh_list="getStockList"
              ></search-tool>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 工具栏 -->
    <div class="sku_header_left">
      <div>
        <el-button
          size="small"
          class="el-icon-plus"
          type="primary"
          @click="createStockOrder"
        >
          创建备货订单
        </el-button>
      </div>
      <div>
        <TableTool
          :search_visible.sync="search_visible"
          @refesh_list="getStockList"
        ></TableTool>
      </div>
    </div>
    <el-table
      :header-cell-style="{
        background: '#fafafa',
        color: '#000000D9',
        fontSize: '14px',
        padding: '10px 0',
        border: '1px solid #eee',
      }"
      :cell-style="{
        color: '#606266',
        fontSize: '12px',
        padding: '6px 0',
      }"
      :data="tableData"
      style="width: 100%"
    >
      <template slot="empty">
        <img
          class="empty-img"
          src="@/assets/imgs/zanwushuju.svg"
          style="width: 64px; height: 50px"
          alt=""
        />
        <div class="noCart">暂无数据</div>
      </template>
      <el-table-column type="expand" label="">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="商品名称">
              <span>{{ props.row.name }}</span>
            </el-form-item>
            <el-form-item label="所属店铺">
              <span>{{ props.row.shop }}</span>
            </el-form-item>
            <el-form-item label="商品 ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="店铺 ID">
              <span>{{ props.row.shopId }}</span>
            </el-form-item>
            <el-form-item label="商品分类">
              <span>{{ props.row.category }}</span>
            </el-form-item>
            <el-form-item label="店铺地址">
              <span>{{ props.row.address }}</span>
            </el-form-item>
            <el-form-item label="商品描述">
              <span>{{ props.row.desc }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" align="center"></el-table-column>
      <el-table-column type="index" align="center" label="#"></el-table-column>
      <el-table-column
        align="center"
        label="录入时间"
        prop="create_time"
      ></el-table-column>
      <el-table-column
        align="center"
        label="所属账号"
        prop="membe"
        width="120"
      ></el-table-column>
      <el-table-column
        align="center"
        label="备货单号"
        prop="receipt_No"
      ></el-table-column>
      <el-table-column
        align="center"
        label="数量"
        prop="amount"
        width="100"
      ></el-table-column>
      <el-table-column
        align="center"
        label="目标仓库"
        prop="warehouse_title"
      ></el-table-column>
      <el-table-column
        align="center"
        label="轨迹状态"
        prop="trackStatus"
        width="260"
      >
        <template slot-scope="scope">
          <div
            style="
              display: flex;
              justify-content: space-around;
              align-items: center;
            "
          >
            <div>
              <div v-if="showIndex.indexOf(scope.row.order_receipt_id) !== -1">
                <template v-for="(item, i) in scope.row.trackStatus">
                  <div>{{ item.msg }}&nbsp;&nbsp;&nbsp;{{ item.time }}</div>
                </template>
              </div>
              <div v-else>
                <template>
                  {{
                    scope.row.trackStatus[scope.row.trackStatus.length - 1].msg
                  }}&nbsp;&nbsp;&nbsp;{{
                    scope.row.trackStatus[scope.row.trackStatus.length - 1].time
                  }}
                </template>
              </div>
            </div>
            <div>
              <i
                @click="expandShopping(scope.row.order_receipt_id)"
                v-if="scope.row.trackStatus.length > 1"
                style="color: #2e82ff; cursor: pointer"
                class="el-icon-d-caret"
              ></i>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="异常件"
        prop="error_msg"
      ></el-table-column>
      <el-table-column
        align="center"
        label="备注"
        width="220"
        prop="remarks"
      ></el-table-column>
      <el-table-column label="操作" width="380px" align="left">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除
          </el-button>
          <el-button icon="el-icon-printer" size="mini" type="primary" @click=""
            >打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <Pagination :child-msg="pageparm" @callFather="callFather"></Pagination>
    <!--创建备货弹窗-->
    <StockOrder
      ref="form"
      @submit="submitClick"
      :dialogVisible.sync="dialogVisible"
      :dialogTitle="dialogTitle"
      @closeForm="closeDialog"
    ></StockOrder>
  </div>
</template>

<script>
import StockOrder from "./components/StockOrder.vue";
import { addStockUp, getStockList } from "@/api/stockup";

export default {
  components: {
    StockOrder,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      searchData: {},
      search_visible: true,
      showIndex: [],
      //---------------------------------------
      loading: false,
      formInline: {
        page: 1,
        limit: 20,
      },
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 20,
        total: 0,
      },
      tableData: [],
    };
  },
  created() {
    this.getStockList();
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    submitClick(val) {
      this.addStockUp(val);
    },
    createStockOrder() {
      this.dialogVisible = true;
      this.dialogTitle = "创建备货订单";
    },
    //关闭弹窗
    closeDialog() {
      this.$refs["form"].form = {};
      this.dialogVisible = false;
    },
    //分页插件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
    },
    expandShopping(e) {
      if (this.showIndex.indexOf(e) === -1) {
        this.showIndex.push(e);
      } else {
        this.showIndex.forEach((item, index) => {
          if (item === e) {
            this.showIndex.splice(index, 1);
          }
        });
      }
    },
    //新增SKU
    async addStockUp(obj) {
      const res = await addStockUp(obj);
      if (res.data.status === 200) {
        this.$message({
          type: "success",
          message: res.data.msg,
        });
      } else {
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    //获取列表数据
    async getStockList() {
      this.loading = true;
      try {
        let obj = {};
        Object.assign(obj, this.searchData, this.formInline);
        const res = await getStockList(obj);
        if (res.data.status === 200) {
          this.tableData = res.data.data.data;
          this.pageparm.currentPage = res.data.data.current_page;
          this.pageparm.total = res.data.data.total;
          this.pageparm.pageSize = res.data.data.per_page;
        } else {
          this.$message.error(res.data.msg);
        }
        this.loading = false;
      } catch (error) {
        this.loading = false;
        this.$message.error("系统错误");
      }
    },
  },
};
</script>
<style scoped>
.el-table {
  border: 1px solid #eee;
  border-top: none;
  border-bottom: none;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.el-form-item {
  margin-bottom: 0;
}

::v-deep .el-form-item__label {
  text-align: center;
}

::v-deep .el-input__inner {
  height: 32px;
  line-height: 32px;
}

.stockUp {
  padding: 10px;
}

.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.el-icon-download {
  margin: 0 8px;
}

.sku_header_left {
  display: flex;
  justify-content: space-between;
  margin: 15px 0;
}
</style>
